<template>
  <div>
    <!-- 三级分类 -->
    <Category :scene="scene" />
    <el-card style="margin: 10px 0">
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="Plus"
          :disabled="!categoryStore.c3Id"
          @click="addSPU"
          >添加SPU</el-button
        >
        <el-table style="margin: 20px 0" border :data="spuRecords">
          <el-table-column
            label="序号"
            align="center"
            type="index"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="SPU操作">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
                @click="addSKU(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="updateSPU(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="View"
                title="查看SKU列表"
                @click="findSku(row)"
              ></el-button>
              <!-- 气泡确认框组件 -->
              <el-popconfirm
                :title="`你确定要删除${row.spuName}?`"
                width="240px"
                @confirm="deleteSpu(row.id)"
              >
                <template #reference>
                  <el-button
                    type="primary"
                    size="small"
                    icon="Delete"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[3, 6, 9, 11]"
          background="true"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @change="getHasSpuData"
        />
      </div>
      <!-- 添加｜修改 SPU 子组件-->
      <SpuForm
        ref="mySpuForm"
        v-show="scene == 1"
        @changeScene="changeScene"
      ></SpuForm>
      <!-- 添加SKU子组件 -->
      <SkuForm
        v-show="scene == 2"
        ref="mySkuForm"
        @changeScene="changeScene"
      ></SkuForm>
      <!-- 对话框 -->
      <el-dialog v-model="show" title="SKU列表">
        <el-table :data="skuArr" border>
          <el-table-column label="sku名字" prop="skuName"></el-table-column>
          <el-table-column label="sku价格" prop="price"></el-table-column>
          <el-table-column label="sku重量" prop="weight"></el-table-column>
          <el-table-column label="sku图片">
            <template #="{ row, $index }">
              <el-image :src="row.skuDefaultImg" />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
//引入组件
import SpuForm from "./components/SpuForm.vue";
import SkuForm from "./components/SkuForm.vue";
import { reactive, ref, watch, onBeforeUnmount } from "vue";
//引入分类仓库
import useCategoryStore from "@/store/modules/category";
import { defineOptions } from "vue";
//引入API
import { reqHasSpuData, reqSkuData, reqDeleteSpu } from "@/api/product/spu";
//引入ts类型
import type { SkuData, SpuData } from "@/api/product/spu/type";
import { ElMessage } from "element-plus";
defineOptions({
  name: "Spu",
});
//应用仓库
let categoryStore = useCategoryStore();
let scene = ref(0); //场景切换的变量
let currentPage = ref(1); //当前页
let pageSize = ref(3); //每页数据个数
let spuRecords = ref<SpuData[]>([]); //储存spu数据
let total = ref(0); //储存总数据
let mySpuForm = ref(); //获取vc组件实例
let mySkuForm = ref(); //获取vc组件实例
let skuArr = reactive<SkuData[]>([]); //用于获取SKU数据的数组
let show = ref<boolean>(false); //对话框的显示与隐藏
//监听categoryStore.c3Id
watch(
  () => categoryStore.c3Id,
  () => {
    if (!categoryStore.c3Id) return;
    getHasSpuData();
  }
);
//监听pageSize改变时currentPage=1
watch([pageSize], () => {
  currentPage.value = 1;
});
//获取已有SPU的数据
const getHasSpuData = async () => {
  let result = await reqHasSpuData(
    currentPage.value,
    pageSize.value,
    categoryStore.c3Id
  );
  if (result.code == 200) {
    spuRecords.value = result.data.records;
    total.value = result.data.total;
  }
};
//添加SPU数据
const addSPU = () => {
  scene.value = 1;
  mySpuForm.value.initAddSpu(categoryStore.c3Id);
};
//修改SPU数据
const updateSPU = (row: SpuData) => {
  scene.value = 1;
  mySpuForm.value.initSpuFormData(row);
};
//删除已有的SPU
const deleteSpu = async (id: number | string) => {
  let result = await reqDeleteSpu(id);
  if (result.code == 200) {
    ElMessage({
      type: "success",
      message: "删除成功",
    });
    //重新获取数据
    getHasSpuData();
  } else {
    ElMessage({
      type: "error",
      message: "删除失败",
    });
  }
};

//添加一个SKU数据
const addSKU = (row: SpuData) => {
  mySkuForm.value.initSkuData(row);
  //切换场景2
  scene.value = 2;
};
//查看SKU数据
const findSku = async (row: SpuData) => {
  let result = await reqSkuData(row.id as number);
  if (result.code == 200) {
    skuArr = result.data;
    show.value = true;
  }
};

//接收子组件传过来的回调
const changeScene = (obj: any) => {
  scene.value = obj.sceneNum;
  if (obj.params == "add") currentPage.value = 1;
  //再次获取新数据重新渲染
  getHasSpuData();
};

//离开组件之前销毁数据
onBeforeUnmount(() => {
  categoryStore.$reset();
});
</script>

<style scoped></style>
